package com.dx.mes.service.tool.service;

import static com.dx.mes.common.bean.def.GenericDef.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.*;
import com.dx.mes.common.table.rtnBean.SimpleRtnBeen;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.his.His_ret_mtrl;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.GUIDGenerator;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.tool.service.IFtplrmagService;
import com.dx.mes.facade.tool.tx.Ftplrmag.FtplrmagI;
import com.dx.mes.facade.tool.tx.Ftplrmag.FtplrmagIA;
import com.dx.mes.facade.tool.tx.Ftplrmag.FtplrmagIB;
import com.dx.mes.facade.tool.tx.Ftplrmag.FtplrmagO;
import com.dx.mes.facade.tool.tx.Ftplrmag.FtplrmagOA;
import com.dx.mes.facade.tool.tx.Ftplrmag.FtplrmagOB;

@Service("ftplrmagService")
@SuppressWarnings({"unchecked"})
public class FtplrmagService implements IFtplrmagService{

	private static final String TX_NAME = T_FTPLRMAG;
	private LogUtils logUtils;
	
	@Autowired
	private IRetMtrlDao retMtrlDao;
	@Autowired
	private IRetWhMtrlDao retWhMtrlDao;
	@Autowired
	private IRetMtrlQrsDao retMtrlQrsDao;
	@Autowired
	private IBisDataDao bisDataDao;
	@Autowired
	private IRetCrtNoDao retCrtNoDao; 
	@Resource
	private SessionFactory sessionFactoryHis;
	@Autowired
	private IBisMtrlDao bisMtrlDao;
	@Autowired
	private IRetToolStDao retToolStDao;
	@Override
	public String subMainProc(String evt_no, String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FtplrmagService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FtplrmagService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");

		String reply ="";
		FtplrmagO outTrx = new FtplrmagO();
		Session session = retMtrlDao.getCrSession();
		try {
			FtplrmagI inTrx = new FtplrmagI();
			
			outTrx.setTrx_id(TX_NAME);
			outTrx.setType_id(TRX_OUT);
			outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
			inTrx = JacksonUtil.fromJson(strInMsg, FtplrmagI.class);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch (action_flg) {
			case ACTION_FLG_QUERY:
				queryByCondition(inTrx,outTrx);
				break;
			case ACTION_FLG_DELETE:
				delWhMtrlFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_ADD:
				addWhMtrlFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_UPDATE:
				updateWhMtrlFnc(inTrx,outTrx);
				break;
			case 'P':
				pickRetWhMtrlFnc(inTrx,outTrx);
				break;
			case 'R':
				returnWhFnc(inTrx,outTrx);
				break;
			case 'S':
				scrpWhMtrlFnc(inTrx,outTrx);
				break;
			case 'I':
				queryRetMtrlByCondition(inTrx,outTrx);
				break;
			case 'C':
				changeRetMtrlFnc(inTrx,outTrx);
				break;
			case 'T':
				queryRetQrsFnc(inTrx,outTrx);
				break;
			case 'F':
				releaseQtimeFnc(inTrx,outTrx);
				break;
			case 'H':
				holdRetMtrlFnc(inTrx,outTrx);
				break;
			case 'N':
				unHoldRetMtrlFnc(inTrx,outTrx);
				break;
			case 'E':
				empMtrlOffFnc(inTrx,outTrx);
				break;
			case 'M':
				mergeMtrlFnc(inTrx,outTrx);
				break;
			case 'G':
				findHisRetMtrlFnc(inTrx,outTrx);
				break;
			case 'J':
					judgeUpdateByCondition(inTrx,outTrx);
					break;
			default:
				errActionFnc(outTrx);
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {

			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.getCause().toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			reply = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + reply + "]");
		}
		return reply;
	}

	private void judgeUpdateByCondition(FtplrmagI inTrx, FtplrmagO outTrx) {
		boolean update_flg = false;
		List<FtplrmagOA> oary= new ArrayList<FtplrmagOA>();
		String  mtrl_unq_id = inTrx.getMtrl_unq_id();
		String hql2 = "FROM Ret_wh_mtrl where mtrl_unq_id = '"+mtrl_unq_id+"'";
		List<Ret_wh_mtrl> list1 = retWhMtrlDao.find(hql2);
		if(list1.size()>0  && list1.get(0).getMtrl_stat().equals(STAT_WH_IN))
			update_flg = true;
		FtplrmagOA oaryA = new FtplrmagOA();
		oaryA.setUpdate_flg(update_flg);
		oary.add(oaryA);
		outTrx.setOaryA(oary);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}


	private void findHisRetMtrlFnc(FtplrmagI inTrx, FtplrmagO outTrx) {
		Session session = sessionFactoryHis.openSession();
		List<FtplrmagOA> oaryList  = new ArrayList<FtplrmagOA>();
		FtplrmagIA iary = inTrx.getIary().get(0);
		String sql = "FROM His_ret_mtrl where mtrl_unq_id = '"+iary.getMtrl_unq_id()+"'";
		Query query = session.createQuery(sql);
		List<His_ret_mtrl> his_ret_mtrls = query.list();
		for (His_ret_mtrl his_ret_mtrl : his_ret_mtrls) {
			FtplrmagOA oaryA = new FtplrmagOA(); 
			oaryA.setMtrl_unq_id(his_ret_mtrl.getMtrl_unq_id());
			oaryA.setMtrl_lot_id(his_ret_mtrl.getMtrl_lot_id());
			oaryA.setMtrl_cus_id_fk(his_ret_mtrl.getMtrl_cus_id_fk());
			oaryA.setMtrl_prod_id_fk(his_ret_mtrl.getMtrl_prod_id_fk());
			oaryA.setMtrl_location(his_ret_mtrl.getMtrl_location());
			oaryA.setMtrl_stat(his_ret_mtrl.getMtrl_stat());
			oaryA.setMtrl_empt_stat(his_ret_mtrl.getMtrl_empt_stat());
			if (null != his_ret_mtrl.getMtrl_qty()) {
				oaryA.setMtrl_qty(String.valueOf(his_ret_mtrl.getMtrl_qty()));
			}
			if (null != his_ret_mtrl.getEvt_timestamp()) {
				oaryA.setEvt_timestamp(his_ret_mtrl.getEvt_timestamp().toString());
			}
			oaryA.setEvt_cate(his_ret_mtrl.getEvt_cate());
			oaryA.setEvt_usr(his_ret_mtrl.getEvt_usr());
			oaryList.add(oaryA);
		}
		outTrx.setOaryA(oaryList);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}


	private void releaseQtimeFnc(FtplrmagI inTrx, FtplrmagO outTrx) {
			Timestamp current_timestamp = Util.getCurrentTimestamp();
			List<FtplrmagIB> iaryB = inTrx.getIaryB();
			for (FtplrmagIB ftplrmagIB : iaryB) {
				Ret_mtrl_qrsId  qrsId = new Ret_mtrl_qrsId();
				qrsId.setMtrl_unq_id(ftplrmagIB.getMtrl_unq_id());
				qrsId.setQrs_id_fk(ftplrmagIB.getQrs_id_fk());
				Ret_mtrl_qrs ret_mtrl_qrs = retMtrlQrsDao.get(qrsId);
				ret_mtrl_qrs.setQrs_reset_timestamp(current_timestamp);
				ret_mtrl_qrs.setQrs_note(ftplrmagIB.getQrs_note());
				retMtrlQrsDao.update(ret_mtrl_qrs);
				retMtrlQrsDao.delete(ret_mtrl_qrs);
			}
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	private void queryRetQrsFnc(FtplrmagI inTrx, FtplrmagO outTrx) {
		FtplrmagIB iaryB = inTrx.getIaryB().get(0);
		String hql = "FROM Ret_mtrl_qrs where mtrl_unq_id = '"+iaryB.getMtrl_unq_id()+"'";
		List<Ret_mtrl_qrs> retMtrlQrsList = retMtrlQrsDao.find(hql);
		List<FtplrmagOB> oaryBList = new ArrayList<FtplrmagOB>();
		for (Ret_mtrl_qrs ret_mtrl_qrs : retMtrlQrsList) {
			FtplrmagOB ftplrmagOB = new FtplrmagOB();
			ftplrmagOB.setMtrl_unq_id(ret_mtrl_qrs.getId().getMtrl_unq_id());
			ftplrmagOB.setQrs_id_fk(ret_mtrl_qrs.getId().getQrs_id_fk());
			ftplrmagOB.setQrs_typ_fk(ret_mtrl_qrs.getQrs_typ_fk());
			if (null != ret_mtrl_qrs.getEvt_timestamp()) {
				ftplrmagOB.setEvt_timestamp(ret_mtrl_qrs.getEvt_timestamp().toString());
			}
			if (null != ret_mtrl_qrs.getQrk_timestamp()) {
				ftplrmagOB.setQrk_timestamp(ret_mtrl_qrs.getQrk_timestamp().toString());
			}
			oaryBList.add(ftplrmagOB);
		}
		outTrx.setOaryB(oaryBList);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}


	private void mergeMtrlFnc(FtplrmagI inTrx, FtplrmagO outTrx) {
		// TODO Auto-generated method stub
		Timestamp current_timeStamp = Util.getCurrentTimestamp();
		String amtrl_unq_id = inTrx.getMtrl_unq_id();
		String hql1= "FROM Ret_mtrl where mtrl_unq_id = '"+amtrl_unq_id+"'";
		List<Ret_mtrl> retMtrlList = retMtrlDao.find(hql1);
		if (retMtrlList.isEmpty()) {
			outTrx.setRtn_code(E_FTPLRMAG_LR_NOT_EXIST+"");
			outTrx.setRtn_mesg("A桶号为[" + amtrl_unq_id+"]的光阻桶不存在!请确认！");
			return;
		}
		Ret_mtrl amtrl = retMtrlList.get(0);
		if (!amtrl.getMtrl_stat().equals(STAT_WAIT)) {
			outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
			outTrx.setRtn_mesg("A桶号为[" + amtrl_unq_id+"]的光阻桶状态不是"+STAT_WAIT+"!请确认！");
			return;
		}
		if (!amtrl.getMtrl_empt_stat().equals(EMPT_STAT_PARTIAL)) {
			outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
			outTrx.setRtn_mesg("A桶号为[" + amtrl_unq_id+"]的光阻桶Empt状态不是"+EMPT_STAT_PARTIAL+"!请确认！");
			return;
		}
		List<FtplrmagIA> iary = inTrx.getIary();
		List<Ret_mtrl> bMtrlList = new ArrayList<Ret_mtrl>();
		for (FtplrmagIA iaryA : iary) {
			String bmtrl_unq_id = iaryA.getMtrl_unq_id();
			String hql2= "FROM Ret_mtrl where mtrl_unq_id = '"+bmtrl_unq_id+"'";
			List<Ret_mtrl> bRetMtrlList = retMtrlDao.find(hql2);
			if (bRetMtrlList.isEmpty()) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_NOT_EXIST+"");
				outTrx.setRtn_mesg("B桶号为[" + bmtrl_unq_id+"]的光阻桶不存在!请确认！");
				return;
			}
			if(amtrl_unq_id.trim().equals(bmtrl_unq_id.trim())){
				outTrx.setRtn_code(E_FTPLRMAG_LR_IS_SAME+"");
				outTrx.setRtn_mesg("A桶号[" + amtrl_unq_id+"]与B桶号["+bmtrl_unq_id+"]相同!请确认！");
				return;
			}
			Ret_mtrl bmtrl = bRetMtrlList.get(0);
			if (!bmtrl.getMtrl_lot_id().equals(amtrl.getMtrl_lot_id())) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_LOT_ERROR+"");
				outTrx.setRtn_mesg("B桶号为[" + bmtrl_unq_id+"]的光阻桶批次号为["+bmtrl.getMtrl_lot_id()+"]和A桶号不一致!请确认！");
				return;
			}
			if (!bmtrl.getMtrl_prod_id_fk().equals(amtrl.getMtrl_prod_id_fk())) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_RPOD_ID_ERROR+"");
				outTrx.setRtn_mesg("B桶号为[" + bmtrl_unq_id+"]的光阻桶料号为["+bmtrl.getMtrl_prod_id_fk()+"]和A桶号不一致!请确认！");
				return;
			}
			if (!bmtrl.getMtrl_cus_id_fk().equals(amtrl.getMtrl_cus_id_fk())) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_RPOD_ID_ERROR+"");
				outTrx.setRtn_mesg("B桶号为[" + bmtrl_unq_id+"]的光阻桶厂商为["+bmtrl.getMtrl_cus_id_fk()+"]和A桶号不一致!请确认！");
				return;
			}
			if (!bmtrl.getMtrl_location().equals(amtrl.getMtrl_location())) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_RPOD_ID_ERROR+"");
				outTrx.setRtn_mesg("B桶号为[" + bmtrl_unq_id+"]的光阻桶位置为["+bmtrl.getMtrl_location()+"]和A桶号不一致!请确认！");
				return;
			}
			if (!bmtrl.getMtrl_stat().equals(amtrl.getMtrl_stat())) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_RPOD_ID_ERROR+"");
				outTrx.setRtn_mesg("B桶号为[" + bmtrl_unq_id+"]的光阻桶PR状态为["+bmtrl.getMtrl_stat()+"]和A桶号不一致!请确认！");
				return;
			}
			if (!bmtrl.getMtrl_empt_stat().equals(amtrl.getMtrl_empt_stat())) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_RPOD_ID_ERROR+"");
				outTrx.setRtn_mesg("B桶号为[" + bmtrl_unq_id+"]的光阻桶Empty状态为["+bmtrl.getMtrl_empt_stat()+"]和A桶号不一致!请确认！");
				return;
			}
			bMtrlList.add(bmtrl);
		}
		int  merge_box_cnt = 0;
		if (!bMtrlList.isEmpty()) {
			amtrl.setMtrl_stat(inTrx.getMtrl_empt_stat());
			for (int i = 0; i < bMtrlList.size(); i++) {
				bMtrlList.get(i).setMtrl_empt_stat(EMPT_STAT_EMPT);
				bMtrlList.get(i).setMtrl_stat(_SPACE);
				bMtrlList.get(i).setEvt_timestamp(current_timeStamp);
				bMtrlList.get(i).setEvt_usr(inTrx.getEvt_usr());
				bMtrlList.get(i).setEvt_cate(EVT_LR_MERGE);
				retMtrlDao.saveOrUpdate(bMtrlList.get(i));
			}
			if(amtrl.getMerge_bot_cnt() != null){
				merge_box_cnt = amtrl.getMerge_bot_cnt();
			}
			amtrl.setMerge_bot_cnt(merge_box_cnt + 1);
			amtrl.setEvt_timestamp(current_timeStamp);
			amtrl.setEvt_usr(inTrx.getEvt_usr());
			amtrl.setEvt_cate(EVT_LR_MERGE);
			retMtrlDao.saveOrUpdate(amtrl);
			
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
		}
	}


	private void 	queryRetMtrlByCondition(FtplrmagI inTrx, FtplrmagO outTrx) {
		List<FtplrmagOA> oaryAList = new ArrayList<FtplrmagOA>();
		FtplrmagIA iaryA = inTrx.getIary().get(0);
		String hql = "FROM Ret_mtrl where 1=1";
		if (!Util.isSpaceCheck(iaryA.getMtrl_unq_id())) {
				hql = hql + " and mtrl_unq_id = '" + iaryA.getMtrl_unq_id() + "'";
		}
		if (!Util.isSpaceCheck(iaryA.getMtrl_lot_id())) {
			hql = hql + " and mtrl_lot_id='" + iaryA.getMtrl_lot_id() + "'";
		}
		if (!Util.isSpaceCheck(iaryA.getMtrl_cus_id_fk())) {
			hql = hql + " and mtrl_cus_id_fk='" + iaryA.getMtrl_cus_id_fk() + "'";
		}
		if (!Util.isSpaceCheck(iaryA.getMtrl_prod_id_fk())) {
			hql = hql + " and mtrl_prod_id_fk='" + iaryA.getMtrl_prod_id_fk() + "'";
		}
		if (!Util.isSpaceCheck(iaryA.getMtrl_location())) {
			hql = hql + " and mtrl_location='" + iaryA.getMtrl_location() + "'";
		}
		if (!Util.isSpaceCheck(iaryA.getMtrl_empt_stat())) {
			hql = hql + " and mtrl_empt_stat='" + iaryA.getMtrl_empt_stat()+ "'";
		}
		if (!Util.isSpaceCheck(iaryA.getMtrl_line())) {
			hql = hql + " and mtrl_location='" + iaryA.getMtrl_line()+ "'";
		}
		if(!Util.isSpaceCheck(iaryA.getMtrl_stat())){
			hql = hql + "and mtrl_stat = '"+iaryA.getMtrl_stat()+"'";
		}
		if (!Util.isSpaceCheck(iaryA.getBeginTime())) {
			hql = hql + " and evt_timestamp >'" + iaryA.getBeginTime()+ "'";
		}
		if (!Util.isSpaceCheck(iaryA.getEndTime())) {
			hql = hql + " and evt_timestamp <'" + iaryA.getEndTime()+ "'";
		}
		List<Ret_mtrl> retMtrlList = retMtrlDao.find(hql);
		for (Ret_mtrl ret_mtrl : retMtrlList) {
			FtplrmagOA oaryA = new FtplrmagOA();
			String qrs_hql = "FROM Ret_mtrl_qrs where mtrl_unq_id = '"+ret_mtrl.getMtrl_unq_id()+"'";
			List<Ret_mtrl_qrs> ret_mtrl_qrss = retMtrlQrsDao.find(qrs_hql);
			for (Ret_mtrl_qrs ret_mtrl_qrs : ret_mtrl_qrss) {
				if ((ret_mtrl_qrs.getQrs_typ_fk().equals("MIN")) && (Util.getCurrentTimestamp().getTime() < ret_mtrl_qrs.getQrk_timestamp().getTime())) {
					oaryA.setQtime_flg(true);
				}else if ((ret_mtrl_qrs.getQrs_typ_fk().equals("MAX")) && (Util.getCurrentTimestamp().getTime() > ret_mtrl_qrs.getQrk_timestamp().getTime())) {
					oaryA.setQtime_flg(true);
				}
			}	
			oaryA.setMtrl_unq_id(ret_mtrl.getMtrl_unq_id());
			oaryA.setMtrl_lot_id(ret_mtrl.getMtrl_lot_id());
			oaryA.setMtrl_cus_id_fk(ret_mtrl.getMtrl_cus_id_fk());
			oaryA.setMtrl_prod_id_fk(ret_mtrl.getMtrl_prod_id_fk());
			oaryA.setMtrl_stat(ret_mtrl.getMtrl_stat());
			oaryA.setMtrl_empt_stat(ret_mtrl.getMtrl_empt_stat());
			oaryA.setMtrl_location(ret_mtrl.getMtrl_location());
			if (null != ret_mtrl.getMtrl_qty()) {
				oaryA.setMtrl_qty(String.valueOf(ret_mtrl.getMtrl_qty()));
			}
			if (null != ret_mtrl.getCur_ice_timestamp()) {
				oaryA.setCur_ice_timestamp(ret_mtrl.getCur_ice_timestamp().toString());
			}
			if (null != ret_mtrl.getEvt_timestamp()) {
				oaryA.setEvt_timestamp(ret_mtrl.getEvt_timestamp().toString());
			}
			if (null != ret_mtrl.getCur_warm_timestamp()) {
				oaryA.setCur_warm_timestamp(ret_mtrl.getCur_warm_timestamp().toString());
			}
			if (null != ret_mtrl.getIce_sts_chg_cnt()) {
				oaryA.setIce_sts_chg_cnt(String.valueOf(ret_mtrl.getIce_sts_chg_cnt()));
			}
			if (null != ret_mtrl.getTool_mount_cnt()) {
				oaryA.setTool_mount_cnt(String.valueOf(ret_mtrl.getTool_mount_cnt()));
			}
			if (null != ret_mtrl.getMerge_bot_cnt()) {
				oaryA.setMerge_bot_cnt(String.valueOf(ret_mtrl.getMerge_bot_cnt()));
			}
			if (null != ret_mtrl.getWarm_1st_timestamp()) {
				long currentLong = Util.getCurrentTimestamp().getTime();
				long warmFirstLong = ret_mtrl.getWarm_1st_timestamp().getTime();
				long total_use_time = (currentLong - warmFirstLong)/(1000*60);
				oaryA.setTotal_use_time(String.valueOf(total_use_time));
			}
			
			oaryAList.add(oaryA);
		}
		outTrx.setOaryA(oaryAList);
		outTrx.setTbl_cnt(retMtrlList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);

	}
	
	private void empMtrlOffFnc(FtplrmagI inTrx, FtplrmagO outTrx) {
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		FtplrmagIA iary = inTrx.getIary().get(0);
		String mtrl_unq_id = iary.getMtrl_unq_id();
		String hql = "FROM Ret_mtrl where mtrl_unq_id = '"+mtrl_unq_id+"'";
		List<Ret_mtrl> retMtrlList = retMtrlDao.find(hql);
		if (retMtrlList.size() == 0) {
			outTrx.setRtn_code(E_FTPLRMAG_LR_NOT_EXIST+"");
			outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻桶不存在!请确认！");
			return;
		}
		Ret_mtrl ret_mtrl = retMtrlList.get(0);
		if (!ret_mtrl.getMtrl_empt_stat().equals(EMPT_STAT_EMPT)) {
			outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
			outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻此时的Empty状态不能为["+ret_mtrl.getMtrl_empt_stat()+"]!请确认！");
			return;
		}
		if (!ret_mtrl.getMtrl_stat().equals(STAT_WAIT)) {
			outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
			outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻此时的状态不能为["+ret_mtrl.getMtrl_stat()+"]!请确认！");
			return;
		}
		if (!ret_mtrl.getMtrl_location().equals(POS_STBK)) {
			outTrx.setRtn_code(E_FTPLRMAG_LR_POS_ERROR+"");
			outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻此时的位置在["+ret_mtrl.getMtrl_location()+"]!不能进行销账！");
			return;
		}
		ret_mtrl.setMtrl_empt_stat(EMPT_STAT_CBAK);
		ret_mtrl.setEvt_timestamp(current_timestamp);
		ret_mtrl.setEvt_usr(inTrx.getEvt_usr());
		ret_mtrl.setEvt_cate(EVT_LR_OFF);
		retMtrlDao.saveOrUpdate(ret_mtrl);
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	
	private void holdRetMtrlFnc(FtplrmagI inTrx, FtplrmagO outTrx) {
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		List<FtplrmagIA> iary = inTrx.getIary();
		
		List<Ret_mtrl> mtrlList = new ArrayList<Ret_mtrl>();
		for (FtplrmagIA iaryA : iary) {
			String mtrl_unq_id = iaryA.getMtrl_unq_id();
			String hql = "FROM Ret_mtrl where mtrl_unq_id = '"+mtrl_unq_id+"'";
			List<Ret_mtrl> retMtrlList = retMtrlDao.find(hql);
			if (retMtrlList.size() == 0) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_NOT_EXIST+"");
				outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻桶不存在!请确认！");
				return;
			}
			Ret_mtrl ret_mtrl = retMtrlList.get(0);
			if (!ret_mtrl.getMtrl_stat().equals(STAT_WAIT)) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
				outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻做HOLD操作时，状态不能为["+ret_mtrl.getMtrl_stat()+"]!请确认！");
				return;
			}
			if (!ret_mtrl.getMtrl_location().equals(POS_STBK)) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_POS_ERROR+"");
				outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻做HOLD操作时，状态不能为["+ret_mtrl.getMtrl_stat()+"]!请确认！");
				return;
			}
			mtrlList.add(ret_mtrl);
		}
		for (Ret_mtrl ret_mtrl : mtrlList) {
			ret_mtrl.setMtrl_stat(STAT_HOLD);
			ret_mtrl.setEvt_timestamp(current_timestamp);
			ret_mtrl.setEvt_usr(inTrx.getEvt_usr());
			ret_mtrl.setEvt_cate(EVT_LR_HOLD);
			retMtrlDao.saveOrUpdate(ret_mtrl);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	
	private void unHoldRetMtrlFnc(FtplrmagI inTrx, FtplrmagO outTrx) {
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		List<FtplrmagIA> iary = inTrx.getIary();
		
		List<Ret_mtrl> mtrlList = new ArrayList<Ret_mtrl>();
		for (FtplrmagIA iaryA : iary) {
			String mtrl_unq_id = iaryA.getMtrl_unq_id();
			String hql = "FROM Ret_mtrl where mtrl_unq_id = '"+mtrl_unq_id+"'";
			List<Ret_mtrl> retMtrlList = retMtrlDao.find(hql);
			if (retMtrlList.size() == 0) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_NOT_EXIST+"");
				outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻桶不存在!请确认！");
				return;
			}
			Ret_mtrl ret_mtrl = retMtrlList.get(0);
			if (!ret_mtrl.getMtrl_stat().equals(STAT_HOLD)) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
				outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻此时的状态不能为["+ret_mtrl.getMtrl_stat()+"]!请确认！");
				return;
			}
			mtrlList.add(ret_mtrl);
		}
		for (Ret_mtrl ret_mtrl : mtrlList) {
			ret_mtrl.setMtrl_stat(STAT_WAIT);
			ret_mtrl.setEvt_timestamp(current_timestamp);
			ret_mtrl.setEvt_usr(inTrx.getEvt_usr());
			ret_mtrl.setEvt_cate(EVT_LR_UNHOLD);
			retMtrlDao.saveOrUpdate(ret_mtrl);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	private void changeRetMtrlFnc(FtplrmagI inTrx, FtplrmagO outTrx) {
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		List<FtplrmagIA> iary = inTrx.getIary();
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		List<Ret_mtrl> mtrlList = new ArrayList<Ret_mtrl>();
		for (FtplrmagIA iaryA : iary) {
			String mtrl_unq_id = iaryA.getMtrl_unq_id();
			String mtrl_location = iaryA.getMtrl_location();
			String mtrl_stat = iaryA.getMtrl_stat();
			String mtrl_empt_stat = iaryA.getMtrl_empt_stat();
//			boolean tool_flg = iaryA.isTool_flg();
			String hql = "FROM Ret_mtrl where mtrl_unq_id = '"+mtrl_unq_id+"'";
			List<Ret_mtrl> retMtrlList = retMtrlDao.find(hql);
			if (retMtrlList.size() == 0) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_NOT_EXIST+"");
				outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻桶不存在!请确认！");
				return;
			}
			Ret_mtrl ret_mtrl = retMtrlList.get(0);
			if (ret_mtrl.getMtrl_stat().equals(STAT_HOLD)) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
				outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻桶状态为HOLD!不能操作！");
				return;
			}

			if (null != ret_mtrl.getMtrl_empt_stat()  && ret_mtrl.getMtrl_empt_stat().equals(EMPT_STAT_EMPT) && (mtrl_stat.equals(STAT_INUSE) || mtrl_location.equals(POS_ICE) || mtrl_location.equals(POS_WARM))) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
				outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻为空桶，不可以做位置变更操作！");
				return;
			}

			/*
			* 通过光阻当前的状态判断是否可以做change操作
			*/
			//当前状态为WAIT
			if (ret_mtrl.getMtrl_stat().equals(STAT_WAIT)) {
				if (!mtrl_stat.equals(STAT_ICE) && !mtrl_stat.equals(STAT_INUSE) ) {
					outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
					outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻桶状态为["+ret_mtrl.getMtrl_stat()+"]!不能将状态变更为["+mtrl_stat+"]！");
					return;
				}else if (mtrl_stat.equals(STAT_ICE)) {
					if (!checkQTime(ret_mtrl, "Q3")) {
						outTrx.setRtn_code(E_FTPLRMAG_LR_QTIME_ERROR+"");
						outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻超过Q3[最大使用时间]！");
						return;
					}else {
						rtnBeen = setQrsDataFnc(ret_mtrl, "Q1", inTrx);
						if(rtnBeen.getlRc() != _NORMAL){
							outTrx.setRtn_code(rtnBeen.getlRc()+"");
							outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
							return;
						}
						int ice_sts_chg_cnt = 1;
						if (null != ret_mtrl.getIce_sts_chg_cnt()) {
							ice_sts_chg_cnt = ret_mtrl.getIce_sts_chg_cnt()+1;
						}
						ret_mtrl.setIce_sts_chg_cnt(ice_sts_chg_cnt);
						ret_mtrl.setCur_ice_timestamp(current_timestamp);
					}
					//TODO:delete Q3time
					rtnBeen = moveQtimeFnc(ret_mtrl,"Q3",inTrx);

				}else if (mtrl_stat.equals(STAT_INUSE)) {
					if (!checkQTime(ret_mtrl, "Q3")) {  //检查是否超过Q3
						outTrx.setRtn_code(E_FTPLRMAG_LR_QTIME_ERROR+"");
						outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻超过Q3[最大使用时间]！");
						return;
					} else {
						//TODO:光阻对应的设备运行模式与mount设备的运行模式是否一致
						Bis_mtrl bis_mtrl = bisMtrlDao.get(ret_mtrl.getMtrl_prod_id_fk());
						if(bis_mtrl != null){
							Ret_tool_st ret_tool_st = retToolStDao.get(mtrl_location);
							if(ret_tool_st != null){
								if(!bis_mtrl.getMtrl_use_tool_run_mode().trim().equals(ret_tool_st.getTool_run_mode().trim())){//不一致报错
									outTrx.setRtn_mesg("光阻对应的设备运行模式["+bis_mtrl.getMtrl_use_tool_run_mode()
											+"]与设备["+mtrl_location+"]对应的运行模式["+ret_tool_st.getTool_run_mode()+"]不符，请确认！");
									outTrx.setRtn_code(String.valueOf(E_FTPLRMAG_LR_RPOD_ID_ERROR));
									return;
								}
							}else{
								outTrx.setRtn_mesg("设备代码["+mtrl_location+"]状态信息不存在，请确认！");
								outTrx.setRtn_code(String.valueOf(E_BIS_MTRL + E_READ_NOT_FOUND));
								return;
							}
						}else{
							outTrx.setRtn_mesg("物料代码["+ret_mtrl.getMtrl_prod_id_fk()+"]不存在，请确认！");
							outTrx.setRtn_code(String.valueOf(E_BIS_MTRL + E_READ_NOT_FOUND));
							return;
						}
						//TODO:查询当前设备的WIP应该使用的光阻物料与mount的光阻是否一致
						int tool_mount_cnt = 1;
						if (null != ret_mtrl.getTool_mount_cnt()) {
							tool_mount_cnt = ret_mtrl.getTool_mount_cnt()+1;
						}
						ret_mtrl.setTool_mount_cnt(tool_mount_cnt);
					}
				}
			}
			//当前状态为 WARM
			//定时器自动跑,check Q2？
			if (ret_mtrl.getMtrl_stat().equals(STAT_WARM)) {
				if (!mtrl_stat.equals(STAT_WAIT)) {
					outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
					outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻桶状态为["+ret_mtrl.getMtrl_stat()+"]!不能将状态变更为["+mtrl_stat+"]！");
					return;
				}
				rtnBeen = setQrsDataFnc(ret_mtrl, "Q3", inTrx);
				if(rtnBeen.getlRc() != _NORMAL){
					outTrx.setRtn_code(rtnBeen.getlRc()+"");
					outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
					return;
				}
				if(!checkQTime(ret_mtrl, "Q2")){
					outTrx.setRtn_code(E_FTPLRMAG_LR_QTIME_ERROR+"");
					outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻未达到最小回温时间[Q2]！");
					return;
				}else {
					rtnBeen = setQrsDataFnc(ret_mtrl, "Q2", inTrx);
					if(rtnBeen.getlRc() != _NORMAL){
						outTrx.setRtn_code(rtnBeen.getlRc()+"");
						outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
						return;
					}
				}

			}
			//当前状态为 INUSE
			if (ret_mtrl.getMtrl_stat().equals(STAT_INUSE)) {
				if(!mtrl_stat.equals(STAT_WAIT)){
					outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
					outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻桶状态为["+ret_mtrl.getMtrl_stat()+"]!不能将状态变更为["+mtrl_stat+"]！");
					return;
				}

			}
			//当前状态为ICE
			if(ret_mtrl.getMtrl_stat().equals(STAT_ICE)){
				if (!mtrl_stat.equals(STAT_WARM)) {
					outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
					outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻桶状态为["+ret_mtrl.getMtrl_stat()+"]!不能将状态变更为["+mtrl_stat+"]！");
					return;
				}else{
//					if (!checkQTime(ret_mtrl, "Q3")) {
//						outTrx.setRtn_code(E_FTPLRMAG_LR_QTIME_ERROR+"");
//						outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻超过Q3[最大使用时间]！");
//						return;
//					}
					rtnBeen = rtWarmFnc(ret_mtrl,outTrx,inTrx);//检查Q1，增加Q2
					if(rtnBeen.getlRc() != _NORMAL){
						outTrx.setRtn_code(rtnBeen.getlRc()+"");
						outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
						return;
					}
				}
			}
			//当前位置为OUT
			if (ret_mtrl.getMtrl_stat().trim().equals(POS_OUT) ) {
				/*if (!checkQTime(ret_mtrl, "Q3")) {
					outTrx.setRtn_code(E_FTPLRMAG_LR_QTIME_ERROR+"");
					outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻超过Q3[最大使用时间]！");
					return;
				}*/
				if (mtrl_stat.equals(STAT_WARM)) {
					rtnBeen = rtWarmFnc(ret_mtrl,outTrx,inTrx);//检查Q1，增加Q2
					if(rtnBeen.getlRc() != _NORMAL){
						outTrx.setRtn_code(rtnBeen.getlRc()+"");
						outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
						return;
					}
				}else if(mtrl_stat.equals(STAT_ICE)){
					//TODO:增加一笔Q1
						rtnBeen = setQrsDataFnc(ret_mtrl, "Q1", inTrx);
						if(rtnBeen.getlRc() != _NORMAL){
							outTrx.setRtn_code(rtnBeen.getlRc()+"");
							outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
							return;
						}
						int ice_sts_chg_cnt = 1;
						if (null != ret_mtrl.getIce_sts_chg_cnt()) {
							ice_sts_chg_cnt = ret_mtrl.getIce_sts_chg_cnt()+1;
						}
						ret_mtrl.setIce_sts_chg_cnt(ice_sts_chg_cnt);
						ret_mtrl.setCur_ice_timestamp(current_timestamp);
				}else{
					outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
					outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻桶状态为["+ret_mtrl.getMtrl_stat()+"]!不能将状态变更为["+mtrl_stat+"]！");
					return;
				}
			}

			if (!Util.isSpaceCheck(mtrl_location)) {
				ret_mtrl.setMtrl_location(mtrl_location);
			}	
			if (!Util.isSpaceCheck(mtrl_stat)) {
				ret_mtrl.setMtrl_stat(mtrl_stat);
			}
			if (!Util.isSpaceCheck(mtrl_empt_stat)) {
				ret_mtrl.setMtrl_empt_stat(mtrl_empt_stat);
			}
			mtrlList.add(ret_mtrl);
		}
		for (Ret_mtrl ret_mtrl : mtrlList) {
			ret_mtrl.setEvt_timestamp(current_timestamp);
			ret_mtrl.setEvt_usr(inTrx.getEvt_usr());
			ret_mtrl.setEvt_cate(EVT_LR_CHANGE_STAT);
			retMtrlDao.saveOrUpdate(ret_mtrl);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	private SimpleRtnBeen moveQtimeFnc(Ret_mtrl ret_mtrl, String qtime,FtplrmagI inTrx) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		String mtrl_unq_id = ret_mtrl.getMtrl_unq_id();
		Ret_mtrl_qrsId qrsId = new Ret_mtrl_qrsId();
		qrsId.setMtrl_unq_id(mtrl_unq_id);
		qrsId.setQrs_id_fk(qtime);
		Ret_mtrl_qrs ret_mtrl_qrs = retMtrlQrsDao.get(qrsId);
		if(null == ret_mtrl_qrs){
			rtnBeen.setlRc(E_FTPLRMAG_LR_QTIME_ERROR);
			rtnBeen.setMsgBuf("桶号为[" + ret_mtrl.getMtrl_unq_id()+"]的光阻桶还没有Q3，无法删除Q3并回冰");
			return rtnBeen;
		}
		ret_mtrl_qrs.setEvt_timestamp(Util.getCurrentTimestamp());
		ret_mtrl_qrs.setEvt_usr(inTrx.getEvt_usr());
		retMtrlQrsDao.delete(ret_mtrl_qrs);
		return  new SimpleRtnBeen(_NORMAL);
	}

	/*
	* 光阻回温的操作
	* */
	private SimpleRtnBeen rtWarmFnc(Ret_mtrl ret_mtrl,FtplrmagO outTrx,FtplrmagI inTrx){
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		Timestamp warm_1st_timestamp = ret_mtrl.getWarm_1st_timestamp();
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		if(null == warm_1st_timestamp){
			ret_mtrl.setWarm_1st_timestamp(current_timestamp);
//			rtnBeen = setQrsDataFnc(ret_mtrl,"Q3",inTrx);
//			if(rtnBeen.getlRc() != _NORMAL){
//				return rtnBeen;
//			}
		}
		if(!checkQTime(ret_mtrl,"Q1")){
			rtnBeen.setlRc(E_FTPLRMAG_LR_QTIME_ERROR);
			rtnBeen.setMsgBuf("桶号为[" + ret_mtrl.getMtrl_unq_id()+"]的光阻桶未达到最小回冰时间[Q1]！");
			return rtnBeen;
		}else{
			rtnBeen = setQrsDataFnc(ret_mtrl,"Q2",inTrx);
			if(rtnBeen.getlRc() != _NORMAL){
				return rtnBeen;
			}
			ret_mtrl.setCur_warm_timestamp(current_timestamp);
		}
		return  new SimpleRtnBeen(_NORMAL);
	}

	private boolean checkQTime(Ret_mtrl ret_mtrl,String qtime) {
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		Ret_mtrl_qrsId qrsId = new Ret_mtrl_qrsId();
		qrsId.setMtrl_unq_id(ret_mtrl.getMtrl_unq_id());
		qrsId.setQrs_id_fk(qtime);
		Ret_mtrl_qrs ret_mtrl_qrs = retMtrlQrsDao.get(qrsId);
		if(ret_mtrl_qrs != null){
			if (ret_mtrl_qrs.getQrs_typ_fk().equals("MIN")) {
				if (null == ret_mtrl_qrs) {
					return true;
				}
				if (ret_mtrl_qrs.getQrk_timestamp().getTime() < current_timestamp.getTime()) {
					return true;
				} else {
					return false;
				}
			} else if (ret_mtrl_qrs.getQrs_typ_fk().equals("MAX")) {
				if (null == ret_mtrl_qrs) {
					return true;
				}
				if (ret_mtrl_qrs.getQrk_timestamp().getTime() < current_timestamp.getTime()) {
					return false;
				} else {
					return true;
				}
			}
		}

		return true;
	}


	private SimpleRtnBeen setQrsDataFnc(Ret_mtrl ret_mtrl,String qtime,FtplrmagI inTrx) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		rtnBeen.setlRc(Integer.valueOf(RETURN_CODE_UNKNOWN));
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		String hql = "FROM Bis_data where data_cate = 'MQRS' and data_id = 'QC01' and data_ext = 'LR' and data_item = '"+qtime+"'";
		Bis_data bis_data = null ;
		List<Bis_data> datas = bisDataDao.find(hql);
		if (datas.size()>0) {
			bis_data = datas.get(0);
		}else{
			rtnBeen.setlRc(E_BIS_DATA + E_READ_NOT_FOUND);
			rtnBeen.setMsgBuf("光阻的Q-Time没有设定，请确认！");
			return  rtnBeen;
		}
		String qrs_type_fk = bis_data.getExt_1();
		String qrs_id_fk = qtime;
		String hourStr = bis_data.getExt_2();
		
		Calendar now = Calendar.getInstance();
		now.add(Calendar.MINUTE, Integer.parseInt(hourStr));
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
		String format = tempDate.format(now.getTime());
		Timestamp qrk_timestamp = Util.String2Timestamp(format);

		Ret_mtrl_qrsId qrsId = new Ret_mtrl_qrsId();
		qrsId.setMtrl_unq_id(ret_mtrl.getMtrl_unq_id());
		qrsId.setQrs_id_fk(qrs_id_fk);
		Ret_mtrl_qrs ret_mtrl_qrs = retMtrlQrsDao.get(qrsId);
		if (ret_mtrl_qrs != null) {
			ret_mtrl_qrs.setEvt_timestamp(current_timestamp);
			ret_mtrl_qrs.setEvt_usr(inTrx.getEvt_usr());
			ret_mtrl_qrs.setQrk_timestamp(qrk_timestamp);
			retMtrlQrsDao.update(ret_mtrl_qrs);
		}else{
			Ret_mtrl_qrs qrs = new Ret_mtrl_qrs();
			qrs.setId(qrsId);
			qrs.setUnq_seq_id(GUIDGenerator.javaGUID());
			qrs.setQrs_typ_fk(qrs_type_fk);
			qrs.setMtrl_prod_id_fk(ret_mtrl.getMtrl_prod_id_fk());
			qrs.setMtrl_lot_id(ret_mtrl.getMtrl_lot_id());
			qrs.setEvt_usr(inTrx.getEvt_usr());
			qrs.setEvt_timestamp(current_timestamp);
			qrs.setQrk_timestamp(qrk_timestamp);
			retMtrlQrsDao.save(qrs);
		}
		rtnBeen.setlRc(_NORMAL);
		return  rtnBeen;
	}


	private void scrpWhMtrlFnc(FtplrmagI inTrx, FtplrmagO outTrx) {
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		List<FtplrmagIA> iary = inTrx.getIary();
		List<Ret_wh_mtrl> ret_wh_mtrls = new ArrayList<Ret_wh_mtrl>();
		List<Ret_mtrl> ret_mtrls = new ArrayList<Ret_mtrl>();
		for (FtplrmagIA iaryA : iary) {
			String hql = "FROM Ret_wh_mtrl where mtrl_unq_id = '"+iaryA.getMtrl_unq_id()+"'";
			List<Ret_wh_mtrl> retWhMtrlList = retWhMtrlDao.find(hql);
			String hql2 = "FROM Ret_mtrl where mtrl_unq_id = '"+iaryA.getMtrl_unq_id()+"'";
			List<Ret_mtrl> retMtrlList = retMtrlDao.find(hql2);
			if (retWhMtrlList.size() == 0 && retMtrlList.size() == 0) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_NOT_EXIST+"");
				outTrx.setRtn_mesg("桶号为[" + iaryA.getMtrl_unq_id()+"]的光阻不存在!请确认！");
				return;
			}else if (retWhMtrlList.size() >1) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_ALREADY_EXIST+"");
				outTrx.setRtn_mesg("桶号为[" + iaryA.getMtrl_unq_id()+"]的光阻重复，请确认！");
				return;
			}
			Ret_wh_mtrl ret_wh_mtrl = retWhMtrlList.get(0);
			if (!ret_wh_mtrl.getMtrl_stat().equals(STAT_WH_IN)){
				if (ret_wh_mtrl.getMtrl_stat().equals(STAT_SCRP)) {
					outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
					outTrx.setRtn_mesg("桶号为[" + iaryA.getMtrl_unq_id()+"]已经报废!请确认！");
					return;
				}else{
					outTrx.setRtn_code(E_FTPLRMAG_LR_POS_ERROR+"");
					outTrx.setRtn_mesg("桶号为[" + iaryA.getMtrl_unq_id()+"]的光阻不是在库状态!请确认！");
					return;
				}
			}


			ret_wh_mtrls.add(ret_wh_mtrl);
			if(retMtrlList != null && retMtrlList.size()>0){
				ret_mtrls.add(retMtrlList.get(0));
			}
		}
		if (ret_wh_mtrls.size() >0 && ret_mtrls.size()>0) {
			for (Ret_mtrl ret_mtrl : ret_mtrls) {
				ret_mtrl.setMtrl_stat(STAT_SCRP);
				ret_mtrl.setMtrl_location(POS_WH);
				ret_mtrl.setEvt_timestamp(current_timestamp);
				ret_mtrl.setEvt_usr(inTrx.getEvt_usr());
				ret_mtrl.setEvt_cate(EVT_LR_SCRAP);
				retMtrlDao.saveOrUpdate(ret_mtrl);
			}
			for (Ret_wh_mtrl ret_wh_mtrl : ret_wh_mtrls) {
				ret_wh_mtrl.setMtrl_stat(STAT_SCRP);
				ret_wh_mtrl.setEvt_timestamp(current_timestamp);
				ret_wh_mtrl.setEvt_usr(inTrx.getEvt_usr());
				ret_wh_mtrl.setEvt_cate(EVT_LR_SCRAP);
				retWhMtrlDao.saveOrUpdate(ret_wh_mtrl);
			}
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
			
		}
	}

	private void returnWhFnc(FtplrmagI inTrx, FtplrmagO outTrx) {
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		List<FtplrmagIA> iary = inTrx.getIary();
		List<Ret_wh_mtrl> ret_wh_mtrls = new ArrayList<Ret_wh_mtrl>();
		List<Ret_mtrl> retMtrlList = new ArrayList<Ret_mtrl>();
		for (FtplrmagIA iaryA : iary) {
			String hql = "FROM Ret_wh_mtrl where mtrl_unq_id = '"+iaryA.getMtrl_unq_id()+"'";
			List<Ret_wh_mtrl> retWhMtrlList = retWhMtrlDao.find(hql);
			String hql2 = "From Ret_mtrl where mtrl_unq_id = '"+iaryA.getMtrl_unq_id()+"'";
			List<Ret_mtrl> ret_mtrls = retMtrlDao.find(hql2);
			if (retWhMtrlList.size() == 0 || ret_mtrls.size() ==0) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_NOT_EXIST+"");
				outTrx.setRtn_mesg("桶号为[" + iaryA.getMtrl_unq_id()+"]的光阻不存在!请确认！");
				return;
			}else if (retWhMtrlList.size() >1) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_ALREADY_EXIST+"");
				outTrx.setRtn_mesg("桶号为[" + iaryA.getMtrl_unq_id()+"]的光阻重复，请确认！");
				return;
			}
			Ret_wh_mtrl ret_wh_mtrl = retWhMtrlList.get(0);
			if (ret_wh_mtrl.getMtrl_stat().equals(STAT_WH_IN)) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_POS_ERROR+"");
				outTrx.setRtn_mesg("桶号为[" + iaryA.getMtrl_unq_id()+"]的光阻不是出库状态!请确认！");
				return;
			};
			Ret_mtrl ret_mtrl = ret_mtrls.get(0);
			if (!ret_mtrl.getMtrl_stat().equals(STAT_WAIT)) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
				outTrx.setRtn_mesg("桶号为[" + iaryA.getMtrl_unq_id()+"]的光阻在Fab区的状态为["+ret_mtrl.getMtrl_stat()+"]!请确认！");
				return;
			}
			ret_wh_mtrl.setMtrl_empt_stat(ret_mtrl.getMtrl_empt_stat());
			ret_wh_mtrls.add(ret_wh_mtrl);
			retMtrlList.add(ret_mtrl);
		}
		if (ret_wh_mtrls.size() >0 && retMtrlList.size() >0) {
			for (Ret_mtrl ret_mtrl : retMtrlList) {
				ret_mtrl.setMtrl_stat(STAT_RTWH);
				ret_mtrl.setMtrl_location(POS_WH);
				ret_mtrl.setEvt_timestamp(current_timestamp);
				ret_mtrl.setEvt_usr(inTrx.getEvt_usr());
				ret_mtrl.setEvt_cate(EVT_LR_RETURN_WH);
				retMtrlDao.saveOrUpdate(ret_mtrl);
			}
			for (Ret_wh_mtrl ret_wh_mtrl : ret_wh_mtrls) {
				ret_wh_mtrl.setMtrl_stat(STAT_WH_IN);

				ret_wh_mtrl.setEvt_timestamp(current_timestamp);
				ret_wh_mtrl.setEvt_usr(inTrx.getEvt_usr());
				ret_wh_mtrl.setEvt_cate(EVT_LR_RETURN_WH);
				retWhMtrlDao.saveOrUpdate(ret_wh_mtrl);
			}

			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
		}
	}

	private void pickRetWhMtrlFnc(FtplrmagI inTrx, FtplrmagO outTrx) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		List<FtplrmagIA> iary = inTrx.getIary();
		List<Ret_wh_mtrl> ret_wh_mtrls = new ArrayList<Ret_wh_mtrl>();
		for (FtplrmagIA iaryA : iary) {
			String hql = "FROM Ret_wh_mtrl where mtrl_unq_id = '"+iaryA.getMtrl_unq_id()+"'";
			String hql2 = "FROM Ret_mtrl where mtrl_unq_id = '"+iaryA.getMtrl_unq_id()+"'";
			List<Ret_wh_mtrl> retWhMtrlList = retWhMtrlDao.find(hql);
			List<Ret_mtrl> retMtrlList = retMtrlDao.find(hql2);
			if (retWhMtrlList.size() == 0) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_NOT_EXIST+"");
				outTrx.setRtn_mesg("桶号为[" + iaryA.getMtrl_unq_id()+"]的光阻不存在!请确认！");
				return;
			}
			Ret_wh_mtrl ret_wh_mtrl = retWhMtrlList.get(0);
			if (!ret_wh_mtrl.getMtrl_stat().equals(STAT_WH_IN)) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_POS_ERROR+"");
				outTrx.setRtn_mesg("桶号为[" + iaryA.getMtrl_unq_id()+"]的不在库!请确认！");
				return;
			};
			ret_wh_mtrls.add(ret_wh_mtrl);
		}
		if (ret_wh_mtrls.size() >0) {
			for (Ret_wh_mtrl ret_wh_mtrl : ret_wh_mtrls) {
				ret_wh_mtrl.setMtrl_stat(WH_POS_OUT);
				ret_wh_mtrl.setEvt_timestamp(current_timestamp);
				ret_wh_mtrl.setEvt_usr(inTrx.getEvt_usr());
				ret_wh_mtrl.setEvt_cate(EVT_LR_PICK);
				retWhMtrlDao.saveOrUpdate(ret_wh_mtrl);
				rtnBeen = addMtrlByWhMtrl(inTrx,ret_wh_mtrl,current_timestamp);
				if(rtnBeen.getlRc() != _NORMAL){
					outTrx.setRtn_code(rtnBeen.getlRc() +"");
					outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
					return;
				}
			}
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
		}
	}

	private void updateWhMtrlFnc(FtplrmagI inTrx, FtplrmagO outTrx) {
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		List<Ret_wh_mtrl> ret_wh_mtrls = new ArrayList<Ret_wh_mtrl>();
		List<FtplrmagIA> iary = inTrx.getIary();
		for (FtplrmagIA iaryA : iary) {
			String mtrl_unq_id = iaryA.getMtrl_unq_id();
			String hql = "FROM Ret_wh_mtrl where mtrl_unq_id = '"+mtrl_unq_id+"'";
			List<Ret_wh_mtrl> retWhMtrlList = retWhMtrlDao.find(hql);
			if (retWhMtrlList.size() == 0) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_NOT_EXIST+"");
				outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻不存在!请确认！");
				return;
			}else if (retWhMtrlList.size() >1) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_ALREADY_EXIST+"");
				outTrx.setRtn_mesg("桶号为[" + iaryA.getMtrl_unq_id()+"]的光阻重复存在，请确认！");
				return;
			}
			Ret_wh_mtrl retWhMtrl = retWhMtrlList.get(0);
			if (!retWhMtrl.getMtrl_stat().equals(STAT_WH_IN)) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_POS_ERROR+"");
				outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻不是在库状态!请确认！");
				return;
			}
			retWhMtrlSetVal(iaryA, current_timestamp, retWhMtrl);
			retWhMtrl.setEvt_usr(inTrx.getEvt_usr());
			ret_wh_mtrls.add(retWhMtrl);
		}
		if (ret_wh_mtrls.size()>0) {
			retWhMtrlDao.saveOrUpdateAll(ret_wh_mtrls);
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
		}
	}

	private void retWhMtrlSetVal(FtplrmagIA iaryA, Timestamp current_timestamp, Ret_wh_mtrl retWhMtrl) {
		retWhMtrl.setMtrl_prod_id_fk(iaryA.getMtrl_prod_id_fk());
		retWhMtrl.setMtrl_cus_id_fk(iaryA.getMtrl_cus_id_fk());
		retWhMtrl.setDest_shop(iaryA.getDest_shop());
//		retWhMtrl.setDest_shop(inTrx.getDest_shop());
		if (!Util.isSpaceCheck(iaryA.getValid_timestamp())) {
			Timestamp ts = new Timestamp(System.currentTimeMillis());
			String valid_timestamp = iaryA.getValid_timestamp()+" 00:00:00";
			ts = Timestamp.valueOf(valid_timestamp);
			retWhMtrl.setValid_timestamp(ts);
			}
		retWhMtrl.setMtrl_qty(Double.parseDouble(iaryA.getMtrl_qty()));
		retWhMtrl.setEvt_timestamp(current_timestamp);
		retWhMtrl.setInwh_timestamp(current_timestamp);
	}

	private void addWhMtrlFnc(FtplrmagI inTrx, FtplrmagO outTrx) {
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		List<Ret_wh_mtrl> ret_wh_mtrls = new ArrayList<Ret_wh_mtrl>();
		List<Ret_mtrl> ret_mtrls = new ArrayList<Ret_mtrl>();
		List<FtplrmagIA> iary = inTrx.getIary();
		String lot_id = this.createlotId(inTrx);
		for (FtplrmagIA iaryA : iary) {
			String mtrl_unq_id = iaryA.getMtrl_unq_id();
			String dest_shop = iaryA.getDest_shop();
			String hql = "FROM Ret_wh_mtrl where mtrl_unq_id = '"+mtrl_unq_id+"'";
			List<Ret_wh_mtrl> retWhMtrlList = retWhMtrlDao.find(hql);
			if (retWhMtrlList.size() > 0) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_ALREADY_EXIST+"");
				outTrx.setRtn_mesg("桶号为[" + mtrl_unq_id+"]的光阻已经存在!请确认！");
				return;
			}
			Ret_wh_mtrl ret_wh_mtrl = new Ret_wh_mtrl();
			ret_wh_mtrl.setMtrl_lot_id(lot_id);
			ret_wh_mtrl.setMtrl_unq_id(mtrl_unq_id);

			ret_wh_mtrl.setMtrl_stat(STAT_WH_IN);
			ret_wh_mtrl.setMtrl_cate(MTRL_CATE_LR);
			ret_wh_mtrl.setMtrl_empt_stat(EMPT_STAT_FULL);
			ret_wh_mtrl.setMtrl_seq_id(GUIDGenerator.javaGUID());
			ret_wh_mtrl.setUnq_seq_id(GUIDGenerator.javaGUID());
			ret_wh_mtrl.setEvt_usr(inTrx.getEvt_usr());
			retWhMtrlSetVal(iaryA, current_timestamp, ret_wh_mtrl);
			ret_wh_mtrls.add(ret_wh_mtrl);

			/*if (iaryA.getDest_shop().equals(STAT_ICE)) {
				String sql = "FROM Ret_mtrl where mtrl_unq_id = '"+mtrl_unq_id+"'";
				List<Ret_mtrl> retMtrlList = retMtrlDao.find(sql);
				if (retMtrlList.size() > 0) {
					return;
				}
				Ret_mtrl ret_mtrl = new Ret_mtrl();
				ret_mtrl.setMtrl_unq_id(ret_wh_mtrl.getMtrl_unq_id());
				ret_mtrl.setMtrl_lot_id(ret_wh_mtrl.getMtrl_lot_id());
				ret_mtrl.setMtrl_prod_id_fk(ret_wh_mtrl.getMtrl_prod_id_fk());
				ret_mtrl.setMtrl_qty(ret_wh_mtrl.getMtrl_qty());
				ret_mtrl.setMtrl_location(POS_OUT);
				ret_mtrl.setMtrl_stat(STAT_ICE);
				ret_mtrl.setEvt_usr(inTrx.getEvt_usr());
				ret_mtrl.setEvt_timestamp(current_timestamp);
				ret_mtrl.setMtrl_seq_id(GUIDGenerator.javaGUID());
				ret_mtrl.setEvt_cate(EVT_LR_ADD_MTRL);
				ret_mtrls.add(ret_mtrl);
			}*/
	}
		if (ret_wh_mtrls.size()>0) {
			retWhMtrlDao.saveOrUpdateAll(ret_wh_mtrls);
//			if (ret_mtrls.size()>0) {
//				retMtrlDao.saveOrUpdateAll(ret_mtrls);
//			}
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
		}
	}
	private SimpleRtnBeen addMtrlByWhMtrl(FtplrmagI inTrx,Ret_wh_mtrl ret_wh_mtrl,Timestamp current_timestamp) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		String mtrl_unq_id = ret_wh_mtrl.getMtrl_unq_id();
		String hql = "FROM Ret_mtrl where mtrl_unq_id = '"+mtrl_unq_id+"'";
		List<Ret_mtrl> retMtrlList = retMtrlDao.find(hql);
		if (retMtrlList.size() > 0 ) {
			if (retMtrlList.get(0).getMtrl_stat().equals(STAT_RTWH) && retMtrlList.get(0).getMtrl_location().equals(POS_WH)) {
				retMtrlList.get(0).setMtrl_stat(inTrx.getMtrl_location());
				retMtrlList.get(0).setMtrl_location(inTrx.getMtrl_location());
				retMtrlList.get(0).setEvt_usr(inTrx.getEvt_usr());
				retMtrlList.get(0).setEvt_timestamp(current_timestamp);
				retMtrlList.get(0).setEvt_cate(EVT_LR_ADD_MTRL);
				retMtrlDao.saveOrUpdate(retMtrlList.get(0));
			} else{
				rtnBeen.setlRc(E_FTPLRMAG_LR_ALREADY_EXIST);
				rtnBeen.setMsgBuf("光阻重复存在！");
				return  rtnBeen;
			}
		}
		Ret_mtrl ret_mtrl = new Ret_mtrl();
		ret_mtrl.setMtrl_unq_id(ret_wh_mtrl.getMtrl_unq_id());
		ret_mtrl.setMtrl_lot_id(ret_wh_mtrl.getMtrl_lot_id());
		ret_mtrl.setMtrl_prod_id_fk(ret_wh_mtrl.getMtrl_prod_id_fk());
		ret_mtrl.setMtrl_cus_id_fk(ret_wh_mtrl.getMtrl_cus_id_fk());
		ret_mtrl.setMtrl_cate(ret_wh_mtrl.getMtrl_cate());
		ret_mtrl.setMtrl_qty(ret_wh_mtrl.getMtrl_qty());
		ret_mtrl.setMerge_bot_cnt(0);
		ret_mtrl.setIce_sts_chg_cnt(0);
		ret_mtrl.setTool_mount_cnt(0);

		ret_mtrl.setMtrl_empt_stat(EMPT_STAT_FULL);
		ret_mtrl.setMtrl_location(inTrx.getMtrl_location());
		ret_mtrl.setMtrl_stat(inTrx.getMtrl_location());
		if (inTrx.getMtrl_location().equals(POS_WARM)){
			FtplrmagO outTrx = new FtplrmagO();
			rtnBeen = rtWarmFnc(ret_mtrl,outTrx,inTrx);
			if(rtnBeen.getlRc() != _NORMAL){
				return rtnBeen;
			}
		}
		ret_mtrl.setMtrl_empt_stat(ret_wh_mtrl.getMtrl_empt_stat());
		ret_mtrl.setEvt_usr(inTrx.getEvt_usr());
		ret_mtrl.setEvt_timestamp(current_timestamp);
		ret_mtrl.setMtrl_seq_id(GUIDGenerator.javaGUID());
		ret_mtrl.setEvt_cate(EVT_LR_ADD_MTRL);
		retMtrlDao.save(ret_mtrl);
		rtnBeen.setlRc(_NORMAL);
		return  rtnBeen;
	}

	private void delWhMtrlFnc(FtplrmagI inTrx, FtplrmagO outTrx) {
		List<FtplrmagIA> iary = inTrx.getIary();
		List<Ret_wh_mtrl> ret_wh_mtrls = new ArrayList<Ret_wh_mtrl>();
		for (FtplrmagIA iaryA : iary) {
			String hql = "FROM Ret_wh_mtrl where mtrl_unq_id = '"+iaryA.getMtrl_unq_id()+"'";
			List<Ret_wh_mtrl> retWhMtrlList = retMtrlDao.find(hql);
			if (retWhMtrlList.size() == 0) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_NOT_EXIST+"");
				outTrx.setRtn_mesg("桶号为[" + iaryA.getMtrl_unq_id()+"]的光阻不存在!请确认！");
				return;
			}else if (retWhMtrlList.size() >1) {
				outTrx.setRtn_code(E_FTPLRMAG_LR_ALREADY_EXIST+"");
				outTrx.setRtn_mesg("桶号为[" + iaryA.getMtrl_unq_id()+"]的光阻重复存在，请确认！");
				return;
			}
			if(!retWhMtrlList.get(0).getMtrl_stat().trim().equals(STAT_WH_IN)){
				outTrx.setRtn_code(E_FTPLRMAG_LR_STAT_ERROR+"");
				outTrx.setRtn_mesg("桶号为[" + iaryA.getMtrl_unq_id()+"]的光阻不是在库状态，不可以删除，请确认！");
				return;
			}
			ret_wh_mtrls.add(retWhMtrlList.get(0));
		}
		retWhMtrlDao.deleteAll(ret_wh_mtrls);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void queryByCondition(FtplrmagI inTrx, FtplrmagO outTrx) {
		List<FtplrmagOA> oaryAList = new ArrayList<FtplrmagOA>();

		String hql = "FROM Ret_wh_mtrl where 1=1 ";
		if (!Util.isSpaceCheck(inTrx.getMtrl_unq_id())) {
				hql = hql + " and mtrl_unq_id = '" + inTrx.getMtrl_unq_id() + "'";
		}
		if (!Util.isSpaceCheck(inTrx.getMtrl_prod_id_fk())) {
			hql = hql + " and mtrl_prod_id_fk='" + inTrx.getMtrl_prod_id_fk() + "'";
		}
		if (!Util.isSpaceCheck(inTrx.getValid_timestamp())) {
			Timestamp ts = new Timestamp(System.currentTimeMillis());
			String valid_timestamp = inTrx.getValid_timestamp()+" 00:00:00";
			ts = Timestamp.valueOf(valid_timestamp);
			hql = hql + " and valid_timestamp='" + ts + "'";
		}
		if (!Util.isSpaceCheck(inTrx.getMtrl_qty())) {
			hql = hql + " and mtrl_qty='" + inTrx.getMtrl_qty() + "'";
		}
		if (!Util.isSpaceCheck(inTrx.getMtrl_lot_id())) {
			hql = hql + " and mtrl_lot_id='" + inTrx.getMtrl_lot_id() + "'";
		}
		if (!Util.isSpaceCheck(inTrx.getMtrl_cus_id_fk())) {
			hql = hql + " and mtrl_cus_id_fk = '" +inTrx.getMtrl_cus_id_fk()+"'";
		}
		if (!Util.isSpaceCheck(inTrx.getDest_shop())) {
			hql = hql + " and dest_shop = '" +inTrx.getDest_shop()+"'";
		}
		List<Ret_wh_mtrl> retWhMtrlList = retWhMtrlDao.find(hql);
		for (Ret_wh_mtrl ret_wh_mtrl : retWhMtrlList) {
		
			FtplrmagOA oaryA = new FtplrmagOA();
			oaryA.setMtrl_unq_id(ret_wh_mtrl.getMtrl_unq_id());
			oaryA.setMtrl_lot_id(ret_wh_mtrl.getMtrl_lot_id());
			oaryA.setMtrl_cus_id_fk(ret_wh_mtrl.getMtrl_cus_id_fk());
			oaryA.setMtrl_prod_id_fk(ret_wh_mtrl.getMtrl_prod_id_fk());
			oaryA.setMtrl_stat(ret_wh_mtrl.getMtrl_stat());
			oaryA.setMtrl_empt_stat(ret_wh_mtrl.getMtrl_empt_stat());
			oaryA.setDest_shop(ret_wh_mtrl.getDest_shop());
			if (null != ret_wh_mtrl.getMtrl_qty()) {
				oaryA.setMtrl_qty(String.valueOf(ret_wh_mtrl.getMtrl_qty()));
			}
			if (null != ret_wh_mtrl.getValid_timestamp()) {
				oaryA.setValid_timestamp(ret_wh_mtrl.getValid_timestamp().toString().substring(0,10));
			}
			if (null != ret_wh_mtrl.getInwh_timestamp()) {
				oaryA.setInwh_timestamp(ret_wh_mtrl.getInwh_timestamp().toString());
			}
			if (null != ret_wh_mtrl.getMtrl_qty()) {
				oaryA.setMtrl_qty(String.valueOf(ret_wh_mtrl.getMtrl_qty()));
			}
			oaryAList.add(oaryA);
		}
		outTrx.setOaryA(oaryAList);
		outTrx.setTbl_cnt(oaryAList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);

	}		
	public static void testQuartzFnc(){
		System.out.println("test Success");
	}
	
	public void errActionFnc(FtplrmagO outTrx) {
			outTrx.setRtn_code("0000009");
			outTrx.setRtn_mesg("Invalid Action_flg");
	}
	private String createlotId(FtplrmagI inTrx){

		String lot_id="";
		Ret_crt_no ret_crt_no = new Ret_crt_no();
		Ret_crt_noId id = new Ret_crt_noId();
		String key_id = this.getStringDate();
		id.setType(LOT_TYPE);
		id.setKey_id(key_id);
		ret_crt_no = retCrtNoDao.get(id);
		if(ret_crt_no == null){
			Ret_crt_no ret_crt_no_new = new Ret_crt_no();
			ret_crt_no_new.setId(id);
			String serial_no = String.format("%03d", 1);
			ret_crt_no_new.setSerial_no(serial_no);
			ret_crt_no_new.setEvt_usr(inTrx.getEvt_usr());
			ret_crt_no_new.setEvt_timestamp(Util.getCurrentTimestamp());
			lot_id = key_id + "001";
			retCrtNoDao.save(ret_crt_no_new);
		}else{
			String old_serial_no = ret_crt_no.getSerial_no();
			int old_serial_no_int = Integer.valueOf(old_serial_no);
			int new_serial_no_int = old_serial_no_int + 1;
			String new_serial_no = String.format("%03d", new_serial_no_int);
			ret_crt_no.setSerial_no(new_serial_no);
			ret_crt_no.setEvt_usr(inTrx.getEvt_usr());
			ret_crt_no.setEvt_timestamp(Util.getCurrentTimestamp());
			retCrtNoDao.update(ret_crt_no);
			lot_id = key_id + new_serial_no;
		}
		return lot_id;
 }
	public  String getStringDate() {  
        Date currentTime = new Date();  
        SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd");  
        String dateString = formatter.format(currentTime);  
        return dateString;  
     }	
	
}
